package synology;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import synology.util.Dom;
import synology.util.SynoConstants;

public class SynoBean
{
	private Element elem;
	private String name;
	private String protocol;
	private String url;
	private String port;
	private String login;
	private String password;


	//------------------------------------------------------------------------------------
	public SynoBean()
	{
	}


	//------------------------------------------------------------------------------------
	public static SynoBean createSynoBean(Element elem)
	{
		SynoBean bean = null;

		try
		{
			bean = new SynoBean(elem);
		}
		catch (Exception e)
		{
		}

		return bean;
	}


	//------------------------------------------------------------------------------------
	private SynoBean(Element elem)
	throws Exception
	{
		this.elem = elem;
		parseXML(elem);
	}


	//------------------------------------------------------------------------------------
	private void parseXML(Element elem)
	throws Exception
	{
		name = getValue(elem, SynoConstants.NAME_NODE);
		protocol = getValue(elem, SynoConstants.PROTOCOL_NODE);
		url = getValue(elem, SynoConstants.URL_NODE);
		port = getValue(elem, SynoConstants.PORT_NODE);
		login = getValue(elem, SynoConstants.LOGIN_NODE);
		password = getValue(elem, SynoConstants.PASSWORD_NODE);
	}


	//------------------------------------------------------------------------------------
	private String getValue(Element elem, String nodeName)
	throws Exception
	{
		NodeList list = elem.getElementsByTagName(nodeName);
		if (list.getLength() != 1)
			throw new Exception("Invalid node");

		Element e = (Element) list.item(0);
		String value = e.getAttribute(SynoConstants.ATTRIBUTS_VALUE);
		value.toString();
		return value;
	}


	//------------------------------------------------------------------------------------
	public Element buildElement()
	throws Exception
	{
		Document doc = Dom.newDocument();
		elem = doc.createElement(SynoConstants.SERVER_NODE);
		Element nameNode = doc.createElement(SynoConstants.NAME_NODE);
		Element protocolNode = doc.createElement(SynoConstants.PROTOCOL_NODE);
		Element urlNode = doc.createElement(SynoConstants.URL_NODE);
		Element portNode = doc.createElement(SynoConstants.PORT_NODE);
		Element loginNode = doc.createElement(SynoConstants.LOGIN_NODE);
		Element passwordNode = doc.createElement(SynoConstants.PASSWORD_NODE);

		nameNode.setAttribute(SynoConstants.ATTRIBUTS_VALUE, name);
		protocolNode.setAttribute(SynoConstants.ATTRIBUTS_VALUE, protocol);
		urlNode.setAttribute(SynoConstants.ATTRIBUTS_VALUE, url);
		portNode.setAttribute(SynoConstants.ATTRIBUTS_VALUE, port);
		loginNode.setAttribute(SynoConstants.ATTRIBUTS_VALUE, login);
		passwordNode.setAttribute(SynoConstants.ATTRIBUTS_VALUE, password);

		elem.appendChild(nameNode);
		elem.appendChild(protocolNode);
		elem.appendChild(urlNode);
		elem.appendChild(portNode);
		elem.appendChild(loginNode);
		elem.appendChild(passwordNode);

		return elem;
	}


	//------------------------------------------------------------------------------------
	public String toString()
	{
		return name + "=" + protocol + "://" + url + ":" + port + "#" + login + ":" + password;
	}


	//------------------------------------------------------------------------------------
	public boolean equals(SynoBean syno)
	{
		if (!name.equals(syno.name))
			return false;
		if (!protocol.equals(syno.protocol))
			return false;
		if (!url.equals(syno.url))
			return false;
		if (!port.equals(syno.port))
			return false;
		if (!login.equals(syno.login))
			return false;
		if (!password.equals(syno.password))
			return false;

		return true;
	}


	//------------------------------------------------------------------------------------
	public void setPassword(String password)
	{
		this.password = password;
	}


	//------------------------------------------------------------------------------------
	public String getPassword()
	{
		return password;
	}


	//------------------------------------------------------------------------------------
	public void setLogin(String login)
	{
		this.login = login;
	}


	//------------------------------------------------------------------------------------
	public String getLogin()
	{
		return login;
	}


	//------------------------------------------------------------------------------------
	public void setPort(String port)
	{
		this.port = port;
	}


	//------------------------------------------------------------------------------------
	public String getPort()
	{
		return port;
	}


	//------------------------------------------------------------------------------------
	public void setUrl(String url)
	{
		this.url = url;
	}


	//------------------------------------------------------------------------------------
	public String getUrl()
	{
		return url;
	}


	//------------------------------------------------------------------------------------
	public void setProtocol(String protocol)
	{
		this.protocol = protocol;
	}


	//------------------------------------------------------------------------------------
	public String getProtocol()
	{
		return protocol;
	}


	//------------------------------------------------------------------------------------
	public void setName(String name)
	{
		this.name = name;
	}


	//------------------------------------------------------------------------------------
	public String getName()
	{
		return name;
	}


	//------------------------------------------------------------------------------------
	public boolean isValid()
	{
		boolean isValid = true;

		if (name == null || name.equals(""))
			isValid = false;

		if (url == null || url.equals(""))
			isValid = false;

		if (protocol == null)
			isValid = false;
		else
		{
			String p = protocol.toLowerCase();
			if (!p.equals("http") && !p.equals("https"))
			{
				isValid = false;
			}
		}

		try
		{
			Integer.parseInt(port);
		}
		catch (Exception e)
		{
			isValid = false;
		}

		if (login == null || login.equals(""))
			isValid = false;

		if (password == null || password.equals(""))
			isValid = false;

		return isValid;
	}
}
